#include "tcpconnection.h"
#include "asyncservice.h"
#include "tcpconnectionfactory.h"
#include <boost/bind.hpp>

TcpConnectionFactory::TcpConnectionFactory(AsyncService &service) :
    m_service(service)
{

}

TcpConnectionFactory::~TcpConnectionFactory()
{

}

TcpConnectionFactory::ConnectionPtr TcpConnectionFactory::create()
{
    void *p = m_pool.alloc(sizeof(TcpConnection));

    if(!p){
        return ConnectionPtr();
    }

    ConnectionPtr _new
            (
                new(p) TcpConnection(m_service),
                boost::bind
                (
                    &TcpConnectionFactory::destroy,
                    this,
                    _1
                )
            );

    return _new;
}

void TcpConnectionFactory::destroy(TcpConnection *p)
{
    if(!p){
        return;
    }

    p->~TcpConnection();

    m_pool.free(p, sizeof(TcpConnection));
}
